package cz.czu.pef.DataModelling.entityObjects;

import java.util.*;


import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 * This entity represents binding between RoleTypeEntityImpl and its TrainingTypes. This entity exists since the list of
 * TrainingTypes for given RoleTypeEntityImpl can be reused by more RoleTypes.
 * 
 * @author rstribrn
 *
 */
@Entity
@Table(name="TRAINING_TYPE_FOR_ROLE_TYPE")
@Inheritance(strategy = InheritanceType.JOINED)
public class TrainingTypeForRoleTypeEntityImpl extends IdPersistenceEntityObjectImpl implements IdPersistenceEntityObject
{
	private static final long serialVersionUID = 1022257034801724764L;

	/*
	 * Entity attributes
	 */
	@ManyToOne(fetch=FetchType.EAGER, optional=true, targetEntity=TrainingTypeEntityImpl.class) // no cascade
	@JoinTable(name="MAP_TT_FRT__TT")
	private TrainingTypeEntityImpl trainingType;

	/*
	 * Relations
	 */
	@ManyToMany(fetch = FetchType.EAGER, targetEntity = RoleTypeEntityImpl.class, mappedBy = "traningTypesForRoleTypes") // no cascade!
	private Set<RoleTypeEntityImpl> roleTypes;

	private long periodicityInSeconds;

	public TrainingTypeForRoleTypeEntityImpl()
	{
		super();
	}

	public TrainingTypeForRoleTypeEntityImpl(TrainingTypeEntityImpl trainingType, long periodicityInSeconds)
	{
		super();
		this.trainingType = trainingType;
		this.periodicityInSeconds = periodicityInSeconds;
	}

	public Set<RoleTypeEntityImpl> getRoleTypes()
	{
		return roleTypes;
	}

	public List<RoleTypeEntityImpl> getRoleTypesAsList()
	{
		return new ArrayList<RoleTypeEntityImpl>(getRoleTypes());
	}

	public long getPeriodicityInSeconds()
	{
		return periodicityInSeconds;
	}

	public void setPeriodicityInSeconds(long periodicityInSeconds)
	{
		this.periodicityInSeconds = periodicityInSeconds;
	}

	public TrainingTypeEntityImpl getTrainingType()
	{
		return trainingType;
	}

    
  public void setTrainingType(TrainingTypeEntityImpl trainingType)
  {
    this.trainingType = trainingType;
  }

  
  public void setRoleTypes(Set<RoleTypeEntityImpl> roleTypes)
  {
    this.roleTypes = roleTypes;
  }

    @Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append("TrainingTypeForRoleTypeEntityImpl [trainingType=");
		builder.append(trainingType);
		builder.append(", roleTypes=");
		builder.append(roleTypes);
		builder.append(", periodicityInSeconds=");
		builder.append(periodicityInSeconds);
		builder.append(", toString()=");
		builder.append(super.toString());
		builder.append("]");
		return builder.toString();
	}

	/* (non-Javadoc)
	 * @see cz.czu.pef.DataModelling.domainObjects.TrainingTypeForRoleType#canBeRemoved()
	 */
	@Override
	public boolean canBeRemoved()
	{
		return getRoleTypes().size() == 0;
	}
}
